package com.oocl.euc.core.maintenance.controller;

import com.oocl.euc.core.builder.CategoryBuilder;
import com.oocl.euc.core.builder.SubCategoryBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Category;
import com.oocl.euc.core.maintenance.model.SubCategory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.Arrays;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class CategoryControllerIT extends ITTestBase{
    @Autowired
    CategoryBuilder categoryBuilder;
    @Autowired
    SubCategoryBuilder subCategoryBuilder;

    @Test
    public void should_return_all_categories() throws Exception {
        this.categoryBuilder.defaultBuilder();
        SubCategory subCategory = subCategoryBuilder.defaultBuilder()
                .withCategory(this.categoryBuilder.getCategory())
                .create();
        Category category = this.categoryBuilder.withSubCategory(Arrays.asList(subCategory)).saveToDB();

        mockMvc.perform(get("/rest/category/categories")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andDo(print())
                .andExpect(jsonPath("$").isNotEmpty())
                .andExpect(jsonPath("$.length()", is(1)))
                .andExpect(jsonPath("$[0].categoryId", is(category.getCategoryId())))
                .andExpect(jsonPath("$[0].categoryName", is(category.getCategoryName())))
                .andExpect(jsonPath("$[0].subCategory.length()", is(1)));
    }
}